package com.jinbo.footmessageworker.person;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.jinbo.footmessageworker.BaseActivity;
import com.jinbo.footmessageworker.MyApplication;
import com.jinbo.footmessageworker.R;
import com.jinbo.footmessageworker.httpbean.ProjectBean;
import com.jinbo.footmessageworker.httpbean.WorkerInformationBean;
import com.jinbo.footmessageworker.httpretrofit.BusinessController;
import com.jinbo.footmessageworker.httpretrofit.Listener;
import com.jinbo.footmessageworker.utils.DialogUtils;
import com.jinbo.footmessageworker.utils.LocalPreferencesHelper;
import com.jinbo.footmessageworker.view.TitleBarView;

import java.util.ArrayList;
import java.util.List;

public class SelectServeItemActivity extends BaseActivity implements View.OnClickListener,CheckItemListViewAdapter.Callbacks {
    private ListView list;
    private List<ProjectBean> projects;
    private int[] new_ids;

    @Override
    public void change(int[] numbers) {
        new_ids=numbers;
    }

    public static void launch(Activity activity, ArrayList<Integer> ids,int code) {

        Intent intent = new Intent(activity, SelectServeItemActivity.class);
        intent.putIntegerArrayListExtra("ids", ids);
        activity.startActivityForResult(intent, code);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_serve_item);
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("我的项目");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        titleBarView.setRightTextIsVisible(true, "保存");
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_rightNextTextLayout, this);
        initView();
        initData();
    }
    private void initData()
    {

        BusinessController.getInstance().getProjects(new Listener<WorkerInformationBean>() {
            @Override
            public void onStart(Object... params) {
                showProgressDialog("请稍后...");
            }

            @Override
            public void onComplete(WorkerInformationBean result, Object... params) {
                dismissProgressDialog();
                projects = result.projects;
                adapter.setprojects(projects);
                adapter.notifyDataSetChanged();
               // Log.d("projects",result.projects.get(0).name);
            }

            @Override
            public void onFail(final String msg, Object... params) {
                dismissProgressDialog();
                if (TextUtils.isEmpty(msg)) {
                } else {
                    show(msg);
                }
            }
        });
    }
    CheckItemListViewAdapter adapter;
    void initView() {
        ArrayList<Integer> ids=getIntent().getIntegerArrayListExtra("ids");
        list=(ListView)findViewById(R.id.list);
        adapter=new CheckItemListViewAdapter(this,projects,ids);
        list.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                new DialogUtils(this, "center", true, 0)
                        .setTitle("提示")
                        .setMessage("是否放弃本次备注操作")
                        .setButtons("取消", "确定",
                                new DialogUtils.OnDialogButtonClickListener() {
                                    @Override
                                    public void onDialogButtonClick(Context context, DialogUtils builder, Dialog dialog, int dialogId, int which) {
                                        if (which == DialogUtils.OnDialogButtonClickListener.BUTTON_RIGHT) {
                                            finish();
                                        }
                                    }
                                }).create().show();
                break;
            case R.id.right_next_text_layout:
                String user_id=MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
               final  String project_ids=getProjectIds(new_ids);
                for(int i=0;i<new_ids.length;i++) {
                    Log.d("项目", new_ids[i]+"");
                }
                Log.d("xiangmu",project_ids);
                BusinessController.getInstance().modifyProjects(user_id,project_ids, new Listener<WorkerInformationBean>(){
                    @Override
                    public void onStart(Object... params) {
                        showProgressDialog("请稍后...");
                    }

                    @Override
                    public void onComplete(WorkerInformationBean result, Object... params) {
                        //Log.d("xiangmu",project_ids);
                        dismissProgressDialog();
                        show("保存成功");
                        setResult(RESULT_OK);
                        // 关闭掉这个Activity
                        finish();
                    }

                    @Override
                    public void onFail(final String msg, Object... params) {
                        dismissProgressDialog();
                        if (TextUtils.isEmpty(msg)) {
                        } else {
                            show(msg);
                        }
                    }
                });
                break;

        }
    }
    String getProjectIds(int[] ids)
    {
        String s="";
        for(int i=0;i<ids.length;i++)
        {
            if(ids[i]!=-1)
                s=s+ids[i]+"-";
        }
        if(!s.equals("")) {
            int length = s.length();
            return s.substring(0, length - 1);
        }
        else
            return s;
    }
/*

    // 改变选择按钮状态
    public void changeState(CheckBox ck) {
        if (mOtherEdt.isFocused()) {
            mOtherEdt.clearFocus();
            KeyBoardUtils.closeKeybord(this, mOtherEdt);
        }
        // isChange=true;
        if (ck.isChecked()) {
            ck.setChecked(false);
        } else {
            ck.setChecked(true);
        }
    }
*/

    // 选择备注的数据，以“/”隔开,（第一个一定是其他输入框，有则是字符串，没有就是“-1”）
/*
    public String getRemarkData() {
        StringBuffer mRemark = new StringBuffer();
        String s = mOtherEdt.getText().toString();
        if (!TextUtils.isEmpty(s)) {
            mRemark.append(s + "/");
        } else {
            mRemark.append("-1/");
        }

        if (mCb1.isChecked()) {
            mRemark.append(mTv1.getText() + "/");
        }
        if (mCb2.isChecked()) {
            mRemark.append(mTv2.getText()+ "/");
        }
        if (TextUtils.isEmpty(mRemark))
            return null;
        else {
            if (mRemark.toString().equals("-1/")) {
                return null;
            } else
                return mRemark.toString();
        }
    }
*/


}
